package com.maddyhome.idea.copyright.ui;

/*
 * Copyright - Copyright notice updater for IDEA
 * Copyright (C) 2004-2005 Rick Maddy. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.AbstractTableCellEditor;
import com.maddyhome.idea.copyright.CopyrightModulePlugin;
import com.maddyhome.idea.copyright.options.Options;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.util.Arrays;
import java.util.Comparator;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

public class ModuleSettingsPanel
{
    public ModuleSettingsPanel(Project project)
    {
        setupControls(project);
    }

    public JComponent getMainComponent()
    {
        return mainPanel;
    }

    public boolean isModified()
    {
        return true;
    }

    public void validate()
    {
    }

    public void apply()
    {
        moduleTable.editingStopped(null);
        ModuleTableModel model = (ModuleTableModel)moduleTable.getModel();
        for (int i = 0; i < model.getRowCount(); i++)
        {
            ModuleData data = (ModuleData)model.getValueAt(i, 1);
            CopyrightModulePlugin plugin = data.getModule().getComponent(CopyrightModulePlugin.class);
            plugin.setOptions(data.getOptions());
        }
    }

    public void setEnabled(boolean enable)
    {
        moduleTable.editingStopped(null);
        moduleScroller.setEnabled(enable);
        moduleTable.setEnabled(enable);
    }

    private void setupControls(Project project)
    {
        ModuleManager mgr = ModuleManager.getInstance(project);
        Module[] modules = mgr.getModules();
        Arrays.sort(modules, new Comparator<Module>()
        {
            public int compare(Module a, Module b)
            {
                return a.getName().compareTo(b.getName());
            }
        });

        Object[][] rowData = new Object[modules.length][2];
        for (int i = 0; i < modules.length; i++)
        {
            CopyrightModulePlugin plugin = modules[i].getComponent(CopyrightModulePlugin.class);
            rowData[i][0] = modules[i].getName();
            rowData[i][1] = new ModuleData(project, modules[i], plugin.getOptions());
        }

        Object[] columns = new Object[]{"Module", "Copyright Settings"};
        ModuleTableModel model = new ModuleTableModel(rowData, columns);

        moduleTable.setModel(model);
        TableColumn column = moduleTable.getColumn(columns[1]);
        //ModuleSettingsRendered renderer = new ModuleSettingsRendered();
        ModuleSettingsEditor editor = new ModuleSettingsEditor();
        //column.setCellRenderer(renderer);
        column.setCellEditor(editor);
        Dimension ps = editor.getPreferredSize();
        column.setPreferredWidth(ps.width);
        moduleTable.setRowHeight(ps.height);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT edit this method OR call it in your
     * code!
     */
    private void $$$setupUI$$$()
    {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayoutManager(1, 1, new Insets(5, 5, 5, 5), -1, -1));
        mainPanel
            .setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Module Level Settings"));
        moduleScroller = new JScrollPane();
        mainPanel.add(moduleScroller, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
            GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
            GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null));
        moduleTable = new JTable();
        moduleTable.setIntercellSpacing(new Dimension(0, 0));
        moduleTable.setRowMargin(0);
        moduleScroller.setViewportView(moduleTable);
    }

    private static class ModuleData
    {
        public ModuleData(Project project, Module module, Options options)
        {
            this.project = project;
            this.module = module;
            this.options = options;
        }

        public Project getProject()
        {
            return project;
        }

        public Module getModule()
        {
            return module;
        }

        public Options getOptions()
        {
            return options;
        }

        public void setOptions(Options options)
        {
            this.options = options;
        }

        public String toString()
        {
            return Options.getStateString(options.getState());
        }

        private Project project;
        private Module module;
        private Options options;
    }

    private static class ModuleTableModel extends AbstractTableModel
    {
        public ModuleTableModel(Object[][] data, Object[] columns)
        {
            this.data = data;
            this.columns = columns;

            orig = new Options[data.length];
            for (int i = 0; i < orig.length; i++)
            {
                try
                {
                    orig[i] = ((ModuleData)data[i][1]).getOptions().clone();
                }
                catch (CloneNotSupportedException e)
                {
                }
            }
        }

        public boolean isModified()
        {
            for (int i = 0; i < getRowCount(); i++)
            {
                if (!((ModuleData)getValueAt(i, 1)).getOptions().equals(orig[i]))
                {
                    return true;
                }
            }

            return false;
        }

        public int getColumnCount()
        {
            return columns.length;
        }

        public int getRowCount()
        {
            return data.length;
        }

        public Object getValueAt(int row, int col)
        {
            return data[row][col];
        }

        public String getColumnName(int col)
        {
            return columns[col].toString();
        }

        public Class getColumnClass(int col)
        {
            return String.class;
        }

        public void setValueAt(Object object, int row, int col)
        {
            ((ModuleData)data[row][1]).setOptions((Options)object);
        }

        public boolean isCellEditable(int row, int col)
        {
            return col == 1;
        }

        private Object[][] data;
        private Options[] orig;
        private Object[] columns;
    }

    private static class ModuleSettingsEditor extends AbstractTableCellEditor
    {
        public ModuleSettingsEditor()
        {
            renderer = new ModuleSettingsCell();
        }

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int col)
        {
            ModuleData data = (ModuleData)value;

            renderer.setData(data.getProject(), data.getModule(), data.getOptions());
            if (isSelected)
            {
                renderer.setBackground(table.getSelectionBackground());
            }
            else
            {
                renderer.setBackground(table.getBackground());
            }

            return renderer.getMainComponent();
        }

        public Object getCellEditorValue()
        {
            return renderer.getOptions();
        }

        public Dimension getPreferredSize()
        {
            return renderer.getMainComponent().getPreferredSize();
        }

        private ModuleSettingsCell renderer;
    }

    private JTable moduleTable;
    private JPanel mainPanel;
    private JScrollPane moduleScroller;
}